﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http.Formatting;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Web.Http;
using System.Web.Http.Dispatcher;
using System.Web.Http.SelfHost;

using Api.Hub.Constraints;
using Api.Hub.MessageHandlers;
using Api.Hub.ConstraintResolvers;
using Api.SelfHost.AssemblyResolvers;

namespace Api.SelfHost
{
    class Program
    {
        static void Main(string[] args)
        {
            HttpSelfHostConfiguration configuration = new HttpSelfHostConfiguration("http://selfhost.me");
            using (HttpSelfHostServer server = new HttpSelfHostServer(configuration))
            {
                //这一步非常重要必须手动加载程序集
                server.Configuration.Services.Replace(typeof(IAssembliesResolver), new PreLoadAssemblyResolver());

                server.Configuration.MessageHandlers.Add(new AddCustomHeader());

                //注册  自定义行内约束  解析器
                server.Configuration.MapHttpAttributeRoutes(new ExtendDefaultConstraintResolver());

                server.Configuration.Routes.MapHttpRoute(
                    name: "DefaultApi",
                    routeTemplate: "api/{controller}/{id}",
                    defaults: new { id = RouteParameter.Optional },
                    constraints: new { id = new Int32RangeConstraint(1, 1000) }
                );

                server.OpenAsync();

                Console.WriteLine("Please map host \"selfhost.me\" to \"127.0.0.1\"");
                Console.WriteLine("Selfhost server has started!");
                Console.WriteLine("Press any key to exit!");

                Console.ReadKey();
            }
        }
    }
}
